<?php
//公共调用json类

namespace App\Common;

class json{



    //若实例化的时候传入相应的值则按新的相应值进行


    public static function push($content){
          $app_key = '09ae7f39eb877836195b52df';        //待发送的应用程序(appKey)，只能填一个。
           $master_secret ='28783d0dacf52cfd12d952a6';    //主密码
            $receiver   ="all";
            $m_type = "http";
            $m_txt = "http://www.groex.cn/";
            $m_time = '86400';//离线保留时间
            $base64 = base64_encode("$app_key:$master_secret");
            $header = array("Authorization:Basic $base64", "Content-Type:application/json");
            $data = array();
            $data['platform'] = 'all';          //目标用户终端手机的平台类型android,ios,winphone
            $data['audience'] = $receiver;      //目标用户
            $data['notification'] = array(
                //统一的模式--标准模式
                "alert" => $content,
                //安卓自定义
                "android" => array(
                    "alert" => $content,
                    "title" => "",
                    "builder_id" => 1,
                    "extras" => array("type" => $m_type, "txt" => $m_txt)
                ),
                //ios的自定义
                "ios" => array(
                    "alert" => $content,
                    "badge" => "1",
                    "sound" => "default",
                    "extras" => array("type" => $m_type, "txt" => $m_txt)
                )
            );

            //苹果自定义---为了弹出值方便调测
            $data['message'] = array(
                "msg_content" => $content,
                "extras" => array("type" => $m_type, "txt" => $m_txt)
            );

            //附加选项
            $data['options'] = array(
                "sendno" => time(),
                "time_to_live" => $m_time, //保存离线时间的秒数默认为一天
                "apns_production" => false, //布尔类型   指定 APNS 通知发送环境：0开发环境，1生产环境。或者传递false和true
            );
            $param = json_encode($data);
            $res = json::push_curl($param, $header);

            if ($res) {       //得到返回值--成功已否后面判断
                return $res;
            } else {          //未得到返回值--返回失败
                return false;
            }

    }

    //推送的Curl方法
    public static function push_curl($param="",$header="") {
        if (empty($param)) { return false; }
        $postUrl = "https://api.jpush.cn/v3/push";   ;
        $curlPost = $param;
        $ch = curl_init();                                      //初始化curl
        curl_setopt($ch, CURLOPT_URL,$postUrl);                 //抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);                    //设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);            //要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);                      //post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        curl_setopt($ch, CURLOPT_HTTPHEADER,$header);           // 增加 HTTP Header（头）里的字段
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);        // 终止从服务端进行验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $data = curl_exec($ch);                                 //运行curl
        curl_close($ch);
        return $data;
    }

    /*
 * 转换大括号，中括号
 */
    public static function value($type){
        switch ($type){
            case '1':
                return (object)array();
                break;
            case '2':
                return array();
                break;
        }
    }
    //转null为""操作

    public static function json_null($str){

        $result = str_replace(":null,",":\"\",",$str);
        $result = str_replace(":null",":\"\"",$result);
        $pos = strpos($result, "\r\n\r\n");
        $response = substr($result, $pos+4);
        return $response;
    }


    /*
     * $error_code     //错误代码
     * $error_msg     //错误报文
     * $data          //数据
     */
    public static function json($error_code,$error_msg,$data){
        $json    = array(
            'result'      => $error_code,
            'msg'       => $error_msg,
            'data'            => $data
        );
        if(is_array($json)){
            return json::json_null(response()->json($json));
        }else{
            return false;
        }
    }

}
